package com.ly;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Aspect
@Component
@Slf4j
public class AopLogSout {

    /**
     * AOP的通知类型：
     *      前置通知
     *      后置通知
     *      环绕通知
     *      异常通知
     *      最终通知
     *      .. 代表当前包和子包
     *      *
     */
    @Around("execution(* com.ly.service.impl..*.*(..))")
    public Object printLogTimerOfService(ProceedingJoinPoint joinPoint) throws Throwable{
        joinPoint.getTarget().getClass();
        log.info("正在执行{}.{}",
                joinPoint.getTarget().getClass(),
                joinPoint.getSignature().getName());
        long startTime = System.currentTimeMillis();
        Object proceed = joinPoint.proceed();
        long endTime = System.currentTimeMillis();
        long exitTime = endTime -startTime;
        if (exitTime > 3000) {
            //使用占位符，不需要使用拼接
            log.error("当前执行耗时：{}",exitTime);
        }else if(exitTime > 2000) {
            log.warn("当前执行耗时：{}",exitTime);
        }else{
            log.info("当前执行耗时：{}",exitTime);
        }
        return proceed;
    }

}
